1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { LeaveRequest } from '../LeaveRequest.entity'; |
3
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
4
|
|
|
import { Leave } from 'src/Domain/HumanResource/Leave/Leave.entity'; |
5
|
|
|
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository'; |
6
|
|
|
import { ICooperativeRepository } from 'src/Domain/Settings/Repository/ICooperativeRepository'; |
7
|
|
|
import { CooperativeNotFoundException } from 'src/Domain/Settings/Repository/CooperativeNotFoundException'; |
8
|
|
|
|
9
|
|
|
export class LeaveRequestToLeavesConverter { |
10
|
|
|
constructor( |
11
|
|
|
@Inject('ILeaveRepository') |
12
|
|
|
private readonly leaveRepository: ILeaveRepository, |
13
|
|
|
@Inject('ICooperativeRepository') |
14
|
|
|
private readonly cooperativeRepository: ICooperativeRepository, |
15
|
|
|
@Inject('IDateUtils') |
16
|
|
|
private readonly dateUtils: IDateUtils |
17
|
|
|
) {} |
18
|
|
|
|
19
|
|
|
public async convert(leaveRequest: LeaveRequest): Promise<void> { |
20
|
|
|
const leaves: Leave[] = []; |
21
|
|
|
const dates = this.dateUtils.getWorkedDaysDuringAPeriod( |
22
|
|
|
new Date(leaveRequest.getStartDate()), |
23
|
|
|
new Date(leaveRequest.getEndDate()) |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
if (!dates || 0 === dates.length) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
const cooperative = await this.cooperativeRepository.find(); |
31
|
|
|
if (!cooperative) { |
32
|
|
|
throw new CooperativeNotFoundException(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
const dayDuration = cooperative.getDayDuration(); |
36
|
|
|
const firstDate = dates[0].toISOString(); |
37
|
|
|
const lastDate = dates[dates.length - 1].toISOString(); |
38
|
|
|
|
39
|
|
|
for (const date of dates) { |
40
|
|
|
leaves.push( |
41
|
|
|
new Leave( |
42
|
|
|
leaveRequest, |
43
|
|
|
this.getTime( |
44
|
|
|
leaveRequest, |
45
|
|
|
dayDuration, |
46
|
|
|
firstDate, |
47
|
|
|
lastDate, |
48
|
|
|
date.toISOString() |
49
|
|
|
), |
50
|
|
|
date.toISOString() |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
await this.leaveRepository.save(leaves); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private getTime( |
59
|
|
|
leaveRequest: LeaveRequest, |
60
|
|
|
dayDuration: number, |
61
|
|
|
firstDate: string, |
62
|
|
|
lastDate: string, |
63
|
|
|
currentDate: string |
64
|
|
|
): number { |
65
|
|
|
return (firstDate === currentDate && |
66
|
|
|
false === leaveRequest.isStartsAllDay()) || |
67
|
|
|
(lastDate === currentDate && false === leaveRequest.isEndsAllDay()) |
68
|
|
|
? dayDuration / 2 |
69
|
|
|
: dayDuration; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|